require 'steem'
class SteemRuby
  def createAccount(params)

    creator = FAUCET_CREATOR
    wif = FAUCET_KEY
    puts params
    new_account_name = params[:user][:account_attributes][:username]
    owner_public_key = params[:owner]
    active_public_key = params[:active]
    posting_public_key = params[:posting]
    memo_public_key = params[:memo]


    options = {
      wif: wif,
      params: {
        fee: '0.000 TESTS',
        creator: creator,
        new_account_name: new_account_name,
        owner: {
          weight_threshold: 1,
          account_auths: [],
          key_auths: [[owner_public_key, 1]],
        },
        active: {
          weight_threshold: 1,
          account_auths: [],
          key_auths: [[active_public_key, 1]],
        },
        posting: {
          weight_threshold: 1,
          account_auths: [],
          key_auths: [[posting_public_key, 1]],
        },
        memo_key: memo_public_key,
        json_metadata: '{}'
      }
    }
    puts options
    # response = Steem::Broadcast.account_create(options)
    # puts response
    begin
      Steem::Broadcast.account_create(options)
    rescue
      {error:"#{$!}"}
    end

  end

  def createAccountWithDelegate(params)

    creator = FAUCET_CREATOR
    wif = FAUCET_KEY
    puts params
    new_account_name = params[:user][:account_attributes][:username]
    owner_public_key = params[:owner]
    active_public_key = params[:active]
    posting_public_key = params[:posting]
    memo_public_key = params[:memo]


    options = {
      wif: wif,
      params: {
        fee: '0.000 TESTS',
        creator: creator,
        new_account_name: new_account_name,
        owner: {
          weight_threshold: 1,
          account_auths: [],
          key_auths: [[owner_public_key, 1]],
        },
        active: {
          weight_threshold: 1,
          account_auths: [],
          key_auths: [[active_public_key, 1]],
        },
        posting: {
          weight_threshold: 1,
          account_auths: [],
          key_auths: [[posting_public_key, 1]],
        },
        memo_key: memo_public_key,
        json_metadata: '{}'
      }
    }
    puts options
    # response = Steem::Broadcast.account_create(options)
    # puts response
    options_share = {
      wif:wif,
      params:{
        delegator:creator,
        delegatee:new_account_name,
        vesting_shares:'0.100000 VESTS',
      }
    }
    begin
      Steem::Broadcast.account_create(options)
      Steem::Broadcast.delegate_vesting_shares(options_share)
    rescue
      {error:"#{$!}"}
    end

  end
end
